var WishUtil = require('./wishUtil');
var Wish = WishUtil.Wish;
var UserWish = WishUtil.UserWish;
var WISH_STATUS = WishUtil.WISH_STATUS;
var WISH_TYPE = WishUtil.WISH_TYPE;

exports.getWishService = function (wishId, callback) {
    WishUtil.getWishFunc({'_id': wishId}, callback);
};

exports.getWishDetailService = function (wishId, callback) {
    WishUtil.getUserWishFunc({'_id': wishId}, callback);
};

exports.getUserWishListService = function (wechatId, callback) {
    WishUtil.getUserWishListFunc({'createdBy': wechatId}, callback);
};

exports.getUserSupportedWishListService = function (wechatId, callback) {
    WishUtil.getUserWishListFunc({'supporters': wechatId}, callback);
};

exports.getJingxuanWishListService = function (callback) {
    WishUtil.getUserWishListFunc({type: WISH_TYPE[2]}, callback);
};

exports.getRandomWishService = function (wxuser, callback) {
    // Get wish that user has not yet made / has not expired/ has not been fulfilled or deleted
    var condition = {createdBy: wxuser, isValid: true, status: WISH_STATUS[1], dateExpired: {$gte: new Date()}};
    UserWish.find(condition, 'wish', function (err, result) {
        if (!err && result != null) {
            var lenn = result.length;
            var wishes = [];
            for (var i = 0; i < lenn; ++i) {
                wishes.push(result[i].wish);
            }
            var query = {'_id': {'$nin': wishes}};
            Wish.find(query, function (err2, result) {
                if (!err2 && result.length > 0) {
                    callback(err2, WishUtil.randomWishAlgorithm(result));
                } else {
                    callback(err2, null);
                }
            });
            //Wish.count(query, function (err2, count) {
            //    if (err2 || count == 0) {
            //        callback(err2, null);
            //    } else {
            //        var rnd = Math.floor(Math.random() * count);
            //        Wish.findOne(query).skip(rnd).exec(function (err3, newwish) {
            //            callback(err3, newwish);
            //        })
            //    }
            //})
        } else {
            callback(err, result);
        }
    })
};

exports.makeWishService = function (wishId, user, callback) {

    Wish.findOne({_id: wishId}, 'maxDays', function (err, result) {
        if (!err && result != null) {
            var temp = new UserWish();
            var curdate = new Date();
            var expiryDate = new Date(curdate);
            expiryDate.setDate(expiryDate.getDate() + result.maxDays);

            temp.wish = wishId;
            temp.createdBy = user;

            //temp.status = 1;
            temp.status = WISH_STATUS[1];
            temp.type = WISH_TYPE[1];

            temp.dateCreated = curdate;
            temp.dateLastModified = temp.dateCreated;

            temp.dateExpired = expiryDate;
            temp.supporters = [];
            temp.isValid = true;

            temp.save(function (err2, result2) {
                callback(err2, result2);
            });
        } else {
            callback(err, null)
        }
    })
};

exports.updateWishService = function (wishId, input, callback) {
    WishUtil.updateUserWishFunc({_id: wishId}, input, callback);
};

exports.removeWishService = function (wishId, callback) {
    WishUtil.updateUserWishFunc({_id: wishId}, {isValid: false}, callback);
    //UserWish.remove({_id: wishId});
};

exports.solvedWishService = function (wishId, callback) {
    WishUtil.updateUserWishFunc({_id: wishId}, {status: WISH_STATUS[4]}, callback);
};

exports.addWishSupporterService = function (user, wishId, callback) {
    WishUtil.updateUserWishFunc({_id: wishId}, {
        $addToSet: {'supporters': user},  // don't use push, since push can result in duplicates
        dateLastModified: new Date()
    }, callback);
};

exports.removeWishSupporterService = function (user, wishId, callback) {
    WishUtil.updateUserWishFunc({_id: wishId}, {
        $pull: {'supporters': user},
        dateLastModified: new Date()
    }, callback);
};
